package com.cheney.common.mybatisplus.config;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.cheney.common.mybatisplus.context.TenantContextHolder;
import com.cheney.common.mybatisplus.filter.MyTenantLineInnerInterceptor;
import com.cheney.common.mybatisplus.properties.TenantProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author si.chen
 * @date 2020/11/17 10:47
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(TenantProperties.class)
// 指定要扫描的Mapper类的包的路径
@MapperScan("com.cheney.**.mapper")
public class MybatisPlusConfig {

    @Autowired
    private TenantProperties tenantProperties;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //多租户配置
        boolean enableTenant = tenantProperties.getEnable();
        if (enableTenant) {
            interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new TenantLineHandler() {
                @Override
                public Expression getTenantId() {
                    String tenant = TenantContextHolder.getTenant();
                    if (tenant != null) {
                        return new StringValue(TenantContextHolder.getTenant());
                    }
                    return new NullValue();
                }

                @Override
                public String getTenantIdColumn() {
                    return tenantProperties.getTenantId();
                }

                /**
                 * 过滤不需要租户管理的表
                 */
                @Override
                public boolean ignoreTable(String tableName) {
                    return tenantProperties.getTenantTables().stream().noneMatch(
                            (e) -> e.equalsIgnoreCase(tableName)
                    );
                }


            }));
        }
        // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //防止全表更新与删除插件: BlockAttackInnerInterceptor
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }
}
